<template>
  <div class="app-container">
    <template>
      <el-alert title="友情提示：消费数据有10分钟延迟，仅统计2022年1月1日后数据！" type="info" />
    </template>
    <div class="filter-container">
      <el-select
        v-model="channels.adminId"
        placeholder="所属渠道"
        clearable
        class="filter-item"
        style="width: 200px; margin-right: 20px"
      >
        <el-option v-for="item in calendarTypeOptions" :key="item.id" :label="item.nickname" :value="item.id" />
      </el-select>

      <span style="margin-right:20px">
        <el-date-picker
          v-model="value1"
          type="date"
          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd"
          placeholder="消费开始日期"
        />
      </span>
      <span>
        <el-date-picker
          v-model="value2"
          type="date"
          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd"
          placeholder="消费结束日期"
        />
      </span>
      <el-input
        v-model="channels.channelCode"
        placeholder="推荐码"
        style="width: 200px; margin-left: 20px"
        class="filter-item"
        @keyup.enter.native="handleFilter"
      />

      <el-button
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        style="margin-left: 20px"
        :loading="downloadLoading"
        @click="handleFilter"
      >
        查询
      </el-button>

      <el-button class="filter-item" style="float: right" type="primary" icon="el-icon-edit" @click="handleCreate">
        创建推荐码
      </el-button>
    </div>
    <el-table :data="list" stripe style="width: 1200px" border show-summary :summary-method="getSummaries">
      <el-table-column prop="channel_code" label="推荐码" width="100" align="center" />
      <el-table-column prop="channel_name" label="名称" width="200" align="center" />
      <el-table-column prop="adminname" label="所属渠道" align="center" />
      <el-table-column prop="room_name" label="默认房间" align="center" />
      <!-- <el-table-column prop="user_count" label="注册数" align="center" /> -->
      <el-table-column prop="anchor_name" label="默认主播" align="center" />
      <el-table-column prop="orderMoney" label="消费金额" align="center">
        <!-- <template slot-scope="{row}">
          {{ row.orderMoney || '/' }}
        </template> -->
      </el-table-column>
      <el-table-column prop="add_time" label="开通时间" align="center" width="200" />
      <el-table-column fixed="right" label="操作" width="100" align="center">
        <template slot-scope="scope" align="center">
          <el-button type="primary" size="small" @click="handleCreate(scope.row)">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="channels.pageNo"
      :limit.sync="channels.pageSize"
      @pagination="handleFilter"
    />
  </div>
</template>

<script>
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
import {
  encryption2
} from '@/api/index'
import Cookies from 'js-cookie'
export default {
  name: 'ComplexTable',
  components: {
    Pagination
  },
  data() {
    return {
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
            picker.$emit('pick', [start, end])
          }
        },
        {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
            picker.$emit('pick', [start, end])
          }
        },
        {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
            picker.$emit('pick', [start, end])
          }
        }
        ]
      },
      value1: '',
      value2: '',
      channels: {
        adminId: '', // 上级渠道ID
        channelCode: '', // 推荐码
        pageNo: 1,
        pageSize: 10
      },
      calendarTypeOptions: [],
      total: 0,
      downloadLoading: false,
      list: []
    }
  },
  created() {
    this.handleFilter()
    this.Channel()
  },
  methods: {
    getSummaries(param) {
      const {
        columns,
        data
      } = param
      const sums = []
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = '合计'
          return
        } else if (index == 5) {
          sums[index] = this.total_money
        } else {
          sums[index] = '-'
        }
      })
      return sums
    },

    Channel() {
      // console.log(JSON.parse(Cookies.get('userinfo')).admin_id);
      const arys = {
        adminId: this.channels.adminId ? this.channels.adminId : JSON.parse(Cookies.get('userinfo')).admin_id
      }
      const url = '/market/singleList'
      encryption2(arys, url).then((res) => {
        if (res.data.status === 0) {
          this.calendarTypeOptions = res.data.result
        } else {
          console.log(res.data.result.text)
        }
      })
    },

    // 查询
    handleFilter() {
      this.downloadLoading = true
      const arys = {
        adminId: this.channels.adminId ? this.channels.adminId : JSON.parse(Cookies.get('userinfo'))
          .admin_id, // 上级渠道ID
        startDate: this.value1 ? this.value1 : '', // 消费开始时间
        endDate: this.value2 ? this.value2 : '', // 消费结束时间
        channelCode: this.channels.channelCode, // 渠道码
        pageNo: this.channels.pageNo,
        pageSize: this.channels.pageSize
      }
      const url = '/market/channelList'
      encryption2(arys, url).then((res) => {
        if (res.data.status === 0) {
          this.list = res.data.result.all_list
          this.total = res.data.result.count
          this.total_money = res.data.result.total_money
        } else {
          this.$message({
            message: res.data.text,
            type: 'error'
          })
        }
        this.downloadLoading = false
      }).catch(err => {
        this.downloadLoading = false
      })
    },
    handleCreate(row) {
      this.$router.push({
        path: '/QueryCode/CreateCode',
        params: {
          Code: row
        },
        name: 'CreateCode'
      })
    }
  }
}
</script>
<style scoped>
  .filter-container .filter-item {
    margin-bottom: 0;
  }

  .el-table--border {
    border-right: 1px solid #dfe6ec;
  }

  .el-alert {
    width: 1200px;
  }

  .filter-container {
    margin-top: 20px;
    width: 1200px;
  }
</style>
